import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
        path: '/login',
        component: () => import("../views/login.vue")
    },
    {
      path:'/',
      redirect:'/login'  
    },
    {
        path: '/home',
        component: () => import("../views/home.vue"),
        redirect: '/census',
        children: [{
            path: '/census',
            component: () => import("../views/census.vue")
        },
        {
            path:'/adduser',
            hidden: true,
            component:()=>import("../views/AddUser.vue")
        },
        {
          path:":id",
          props: true,
          component:()=>import("../views/components/model.vue")  
        },
        {
          path:'/editoruser/:id',
          name:'editor',
          props: { default: true, sidebar: false },
          component:()=>import("../views/editoruser.vue"),
        },
        {
            path:'/list',
            component:()=>import("../views/list.vue")
        },
        {
            path:'/sushi',
            component:()=>import("../views/components/sushi.vue")
        },
        {
            path:'/consume',
            component:()=>import("../views/consume.vue")
        },
        {
            path:'/recharge',
            component:()=>import("../views/recharge.vue")
        },
        {
            path:'/record',
            name:'record',
            component:()=>import("../views/record.vue")
        }
    ]
    }, {
        path: '/error/:id',
        component: () => import("../views/Error.vue")
    }
    ]
});






import { checkToken, editor } from "../api/index"
//验证token有效性 方法
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        let token = localStorage.getItem('token');
        if (token === null || token === '') {
            next('/login');
        } else {
            checkToken(token).then(res => {
                if (res) {
                    next()
                }
                else {
                    next('/error')
                }
            })
        }
    }
});

export default router